

package com.hazelcast.spi.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Represents the range of IPv4 Ports.
 */
public final class PortRange {
    private static final Pattern PORT_NUMBER_REGEX = Pattern.compile("^(\\d+)$");
    private static final Pattern PORT_RANGE_REGEX = Pattern.compile("^(\\d+)-(\\d+)$");

    private static final int MIN_PORT = 0;
    private static final int MAX_PORT = 65535;

    private final int fromPort;
    private final int toPort;

    /**
     * Creates {@link PortRange} from the {@code spec} String.
     *
     * @param spec port number (e.g "5701") or port range (e.g. "5701-5708")
     * @throws IllegalArgumentException if the specified spec is not a valid port or port range
     */
    public PortRange(String spec) {
        Matcher portNumberMatcher = PORT_NUMBER_REGEX.matcher(spec);
        Matcher portRangeMatcher = PORT_RANGE_REGEX.matcher(spec);
        if (portNumberMatcher.find()) {
            int port = Integer.parseInt(spec);
            this.fromPort = port;
            this.toPort = port;
        } else
            if (portRangeMatcher.find()) {
                this.fromPort = Integer.parseInt(portRangeMatcher.group(1));
                this.toPort = Integer.parseInt(portRangeMatcher.group(2));
            } else {
                throw new IllegalArgumentException(String.format("Invalid port range specification: %s", spec));
            }

        validatePorts();
    }

    private void validatePorts() {
        if (fromPort < MIN_PORT || fromPort > MAX_PORT) {
            throw new IllegalArgumentException(String.format("Specified port (%s) outside of port range (%s-%s)", fromPort, MIN_PORT, MAX_PORT));
        }
        if (toPort < MIN_PORT || toPort > MAX_PORT) {
            throw new IllegalArgumentException(String.format("Specified port (%s) outside of port range (%s-%s)", toPort, MIN_PORT, MAX_PORT));
        }
        if (fromPort > toPort) {
            throw new IllegalArgumentException(String.format("Port %s is greater than %s", fromPort, toPort));
        }
    }

    public int getFromPort() {
        return fromPort;
    }

    public int getToPort() {
        return toPort;
    }

    @Override
    public String toString() {
        return String.format("%d-%d", fromPort, toPort);
    }
}
